
P = {
    //IS_PORTRAIT: true,// true:portrait, false:landscape
    PAGE_GAP: 50,
    PAGE_W: 0,
    FPS: 30,
    DISP_DURATION: 4000,// 每页默认显示时长
    DELAY_1: 400,// 延迟1
    dispStatus: 0,// 0:pause, 1:play

    imgCache:[],
    curPageIdx:0,
    loadedPageCnt:0,
    dispTimer: null
};

var P_TOUCH = {
    startX: 0,
    startY: 0,
    startPos: 0,
    offset: 0,
    bQuickSlideFlg: false,// 是否快速滑动
    autoSlider: null,

    TO_NEXT: -1,
    TO_CURR: 0,
    TO_PRE: 1
};

var gPlayer = null;
var gMediaWhRatio=2/3;// 视频宽高比
var gMediaH = 200;// 视频当前高
var MediaPos = Number(gMediaPos) || 3;

var txt;

$(function(){
    // 监听横竖屏切换
    var init=function(){
        updateOri();
        if(supportOri()){
            window.addEventListener("orientationchange",updateOri,false);
        }else{
            window.addEventListener("resize", winResize,false);
        }
    };
    window.addEventListener("DOMContentLoaded",init,false);

    txt = document.getElementById('txt');
    document.getElementById('timeout_txt').innerHTML="因网速限制音视频加载超时，<br>PPT将以图文形式播放。<br>(可尝试刷新页面以多媒体形式播放)";

    // 加载图片
    P.loadedPageCnt = 0;
    for(var i=0; i<gPagesJson.length;i++){
        var pageImg = new Image();

        if (0 == i) {
            // 首页加载完毕则可以画canvas
            //pageImg.addEventListener('load', function eventLoaded() {
            //    P.loadedPageCnt++;
            //    if (isCacheEnd()){
            //        //alert("Images loaded");// for test
            //    }
            //    gIsPortrait = isPortrait();
            //    P_TOUCH.offset = 0;
            //    updCanv(0);
            //}, false);
            pageImg.onload = function () {
                P.loadedPageCnt++;
                gShareIcon = gPagesJson[0].thumb+gImgParam;

                gIsPortrait = isPortrait();
                P_TOUCH.offset = 0;
                updCanv(0);
                $(".disp_play").removeClass("display_hide");
            };

        } else {
            //pageImg.addEventListener('load', function eventLoaded() {
            //    P.loadedPageCnt++;
            //    if (isCacheEnd()){
            //        //alert("Images loaded");// for test
            //    }
            //}, false);
            pageImg.onload = function () {
                P.loadedPageCnt++;
            };
        }

        pageImg.src = gPagesJson[i].thumb+gImgParam;
        P.imgCache.push(pageImg);
    }

    // 绑定touch事件
    bindTouchEvent();
});

function updateOri(){
    gIsPortrait = isPortrait();
    P_TOUCH.offset = 0;
    updCanv(0);
    setMediaPos();

    // 项目调整
    if (gIsPortrait) {
        $("#music_div").removeClass("music");
        $("#music_div").addClass("music_rot90");
        var icon = document.getElementById('icon-music');
        icon.style.height = document.body.clientWidth/10+"px";
        icon.style.width = icon.style.height;

        $("#logo").removeClass("logo");
        $("#logo").addClass("logo_rot90");

        $(".disp_icon").addClass("disp_icon_rot90");
        $(".disp_icon").removeClass("disp_icon");

        //$("#load_img").removeClass("breath2");
        //$("#load_img").addClass("breath3");

        var lt = document.getElementById('load_txt');
        lt.style.fontSize = document.body.clientWidth/16+"px";
        lt.style.width = document.body.clientHeight/2+"px";
        //$("#load_txt").removeClass("breath2");
        //$("#load_txt").addClass("breath3");
        $("#load_txt").removeClass("load_txt");
        $("#load_txt").addClass("load_txt_rot90");

        var tt = document.getElementById('timeout_txt');
        tt.style.fontSize = document.body.clientWidth/18+"px";
        tt.style.width = document.body.clientHeight*0.8+"px";
        $("#timeout_txt").addClass("rot90");

        var to = document.getElementById('timeout_ok');
        to.style.height = document.body.clientWidth/10+"px";
        $("#timeout_ok").addClass("rot90_2");

    } else {
        $("#music_div").removeClass("music_rot90");
        $("#music_div").addClass("music");
        var icon = document.getElementById('icon-music');
        icon.style.height = document.body.clientHeight/10+"px";
        icon.style.width = icon.style.height;

        $("#logo").removeClass("logo_rot90");
        $("#logo").addClass("logo");

        $(".disp_icon_rot90").addClass("disp_icon");
        $(".disp_icon_rot90").removeClass("disp_icon_rot90");

        //$("#load_img").removeClass("breath3");
        //$("#load_img").addClass("breath2");

        var lt = document.getElementById('load_txt');
        lt.style.fontSize = document.body.clientHeight/16+"px";
        lt.style.width = document.body.clientWidth/2+"px";
        //$("#load_txt").removeClass("breath3");
        //$("#load_txt").addClass("breath2");
        $("#load_txt").removeClass("load_txt_rot90");
        $("#load_txt").addClass("load_txt");

        var tt = document.getElementById('timeout_txt');
        tt.style.fontSize = document.body.clientHeight/18+"px";
        tt.style.width = document.body.clientWidth*0.8+"px";
        $("#timeout_txt").removeClass("rot90");

        var to = document.getElementById('timeout_ok');
        to.style.height = document.body.clientHeight/10+"px";
        $("#timeout_ok").removeClass("rot90_2");
    }
};

function winResize(){
    updateOri();
}

function isCacheEnd(){
    if(P.loadedPageCnt!= gPagesJson.length){
        return false;
    }
    for(var i=0;i< P.imgCache.length;i++){
        if(!P.imgCache[i]){
            return false;
        }
    }
    return true;
}

function bindTouchEvent(){
    var tag = document.getElementById('canv_div');
    tag.addEventListener('touchstart',function (e) {
        _touchStart(e);
    },false);
    tag.addEventListener('touchmove',function (e) {
        _touchMove(e);
        e.stopPropagation();
        e.preventDefault();
    },false);
    tag.addEventListener('touchend',function (e) {
        _touchEnd(e);
    },false);
    tag.addEventListener('touchcancel',function (e) {
        _touchEnd(e);
    },false);
}

function _touchStart(e){
    if (P_TOUCH.autoSlider){
        clearInterval(P_TOUCH.autoSlider);
        P_TOUCH.autoSlider = null;
    }

    P_TOUCH.bQuickSlideFlg = false;

    if(e.touches.length >= 1){
        P_TOUCH.bQuickSlideFlg = true;
        setTimeout(function(){
            P_TOUCH.bQuickSlideFlg = false;
        }, 200);

        P_TOUCH.startX=e.touches[0].pageX;
        P_TOUCH.startY=e.touches[0].pageY;

        gIsPortrait = isPortrait();
        P_TOUCH.startPos = (gIsPortrait) ? e.touches[0].pageY : e.touches[0].pageX;
    }
}

function _touchMove(e){
    //if (P_TOUCH.autoSlider) {
    //    // 自动翻页中
    //    return;
    //}

    if(e.touches.length >=1){
        //var gx=Math.abs(e.touches[0].pageX - P_TOUCH.startX);
        //var gy=Math.abs(e.touches[0].pageY - P_TOUCH.startY);
        var curPos = (gIsPortrait) ? e.touches[0].pageY : e.touches[0].pageX;
        P_TOUCH.offset = (curPos-P_TOUCH.startPos);

        updCanv(P_TOUCH.offset);
    }
}

function _touchEnd(e){
    var bGoSlide = false;
    if ((P_TOUCH.bQuickSlideFlg && Math.abs(P_TOUCH.offset)>P.PAGE_W/20) ||// 快速滑动时
        Math.abs(P_TOUCH.offset) >= P.PAGE_W/2) {// 慢速滑动时
        bGoSlide = true;
    }

    if (P_TOUCH.offset <= 0) {
        // to next
        if (bGoSlide && P.curPageIdx < P.imgCache.length-1) {
            autoSlide(P_TOUCH.TO_NEXT,onSlideChangeEnd);
        } else {
            autoSlide(P_TOUCH.TO_CURR,onSlideChangeEnd);
        }
    } else {
        // to pre
        if (bGoSlide && P.curPageIdx > 0) {
            autoSlide(P_TOUCH.TO_PRE,onSlideChangeEnd);
        } else {
            autoSlide(P_TOUCH.TO_CURR,onSlideChangeEnd);
        }
    }
}

function supportOri(){
    return (typeof window.orientation == "number" && typeof window.onorientationchange == "object");
}
// 横竖屏判断
function isPortrait(){
    var ret = true;
    if (supportOri()) {
        var orientation = window.orientation;
        switch(orientation){
            case 90:
            case -90:
                ret = false;// landscape
                break;
            default:
                ret = true;// portrait
        }

    } else {
        var w=document.body.clientWidth;
        var h=document.body.clientHeight;
        ret = (w > h)? false:true;
    }
    return ret;
}

function setCanvPos(canv){
    // 设定视频位置
    var div = document.getElementById('canv_div');
    var top = 0;
    var left = 0;
    top = (document.body.clientHeight - canv.height) * 0.5;
    left = (document.body.clientWidth - canv.width) * 0.5;
    div.style.top = top + "px";
    div.style.left = left + "px";
    div.style.width = canv.width + "px";
    div.style.height = canv.height + "px";
}

function updCanv(offset){

    // for TEST
    //if (gIsPortrait)
    //    document.body.style.background = "#ffffff";
    //else
    //    document.body.style.background = "#ffffff";

    if (!P.imgCache || P.imgCache.length<1 || P.loadedPageCnt<1) {
        return;
    }

    var w=document.body.clientWidth;
    var h=document.body.clientHeight;

    var canv = document.getElementById('canv');
    var cont = canv.getContext('2d');

    if (gIsPortrait) {
        if ((h/w) > (16/9)) {
            canv.width = w;
            canv.height = w * 16/9;
        } else {
            canv.height = h;
            canv.width = h * 9/16;
        }
        var D = Math.PI / 2;
        cont.rotate(D);
        //cont.setTransform(Math.cos(D),Math.sin(D),-Math.sin(D),Math.cos(D),0,0);
        P.PAGE_W = canv.height;
    } else {
        if ((w/h) > (16/9)) {
            canv.height = h;
            canv.width = h * 16/9;
        } else {
            canv.width = w;
            canv.height = w * 9/16;
        }
        P.PAGE_W = canv.width;
    }

    setCanvPos(canv);

    if (gIsPortrait) {
        // Context已旋转过90度

        // 等比缩放图片撑满canvas不留白
        var scaleW = canv.height/ P.imgCache[P.curPageIdx].width;
        var scaleH = canv.width/ P.imgCache[P.curPageIdx].height;
        var s = (scaleW>scaleH)?scaleW:scaleH;
        var x = -(P.imgCache[P.curPageIdx].width*s-canv.height)/2;
        var y = -(P.imgCache[P.curPageIdx].height*s-canv.width)/2;
        //console.log("w:"+w+", h:"+h+", canvW:"+canv.width+", canvH:"+canv.height, "scale:"+s);

        cont.lineWidth = 2;
        cont.strokeStyle = 'black';

        // 下一页
        if (offset<0 && P.imgCache[P.curPageIdx+1]){
            // 间隔
            cont.clearRect(x+offset+P.imgCache[P.curPageIdx].width*s,y-canv.width,P.PAGE_GAP,canv.width);
            cont.drawImage(P.imgCache[P.curPageIdx+1],
                0, 0,
                P.imgCache[P.curPageIdx+1].width, P.imgCache[P.curPageIdx+1].height,
                x+offset+P.PAGE_GAP+P.imgCache[P.curPageIdx].width*s, y-canv.width,
                P.imgCache[P.curPageIdx+1].width*s,P.imgCache[P.curPageIdx+1].height*s);
            // 描边
            //cont.strokeRect(x+offset+P.PAGE_GAP+P.imgCache[P.curPageIdx].width*s+cont.lineWidth/2,
            //    y-canv.width+cont.lineWidth/2,
            //    P.imgCache[P.curPageIdx+1].width*s-cont.lineWidth,
            //    P.imgCache[P.curPageIdx+1].height*s-cont.lineWidth);
        }
        // 上一页
        else if (offset>0 && P.imgCache[P.curPageIdx-1]){
            // 间隔
            cont.clearRect(x+offset-P.PAGE_GAP,y-canv.width,P.PAGE_GAP,canv.width);
            cont.drawImage(P.imgCache[P.curPageIdx-1],
                0, 0,
                P.imgCache[P.curPageIdx-1].width, P.imgCache[P.curPageIdx-1].height,
                x+offset-P.PAGE_GAP-P.imgCache[P.curPageIdx-1].width*s, y-canv.width,
                P.imgCache[P.curPageIdx-1].width*s,P.imgCache[P.curPageIdx-1].height*s);
            // 描边
            //cont.strokeRect(x+offset-P.PAGE_GAP-P.imgCache[P.curPageIdx-1].width*s+cont.lineWidth/2,
            //    y-canv.width+cont.lineWidth/2,
            //    P.imgCache[P.curPageIdx-1].width*s-cont.lineWidth,
            //    P.imgCache[P.curPageIdx-1].height*s-cont.lineWidth);
        }

        // 本页
        cont.drawImage(P.imgCache[P.curPageIdx],
            0, 0, P.imgCache[P.curPageIdx].width, P.imgCache[P.curPageIdx].height,
            x+offset, y-canv.width,P.imgCache[P.curPageIdx].width*s,P.imgCache[P.curPageIdx].height*s);
        // 描边
        //cont.strokeRect(x+offset+cont.lineWidth/2, y-canv.width+cont.lineWidth/2,
        //    P.imgCache[P.curPageIdx].width*s-cont.lineWidth,
        //    P.imgCache[P.curPageIdx].height*s-cont.lineWidth);

    } else {
        // 等比缩放图片撑满canvas不留白
        var scaleW = canv.width/P.imgCache[P.curPageIdx].width;
        var scaleH = canv.height/P.imgCache[P.curPageIdx].height;
        var s = (scaleW>scaleH)?scaleW:scaleH;
        var x = -(P.imgCache[P.curPageIdx].width*s-canv.width)/2;
        var y = -(P.imgCache[P.curPageIdx].height*s-canv.height)/2;

        //console.log("w:"+w+", h:"+h+", canvW:"+canv.width+", canvH:"+canv.height, "scale:"+s);

        // 本页
        cont.drawImage(P.imgCache[P.curPageIdx],
            0, 0,
            P.imgCache[P.curPageIdx].width, P.imgCache[P.curPageIdx].height,
            x+offset, y,
            P.imgCache[P.curPageIdx].width*s, P.imgCache[P.curPageIdx].height*s);
        // 描边
        //cont.strokeRect(x+offset+cont.lineWidth/2, y+cont.lineWidth/2,
        //    P.imgCache[P.curPageIdx].width*s-cont.lineWidth,
        //    P.imgCache[P.curPageIdx].height*s-cont.lineWidth);

        // 下一页
        if (offset<0 && P.imgCache[P.curPageIdx+1]){
            // 间隔
            cont.clearRect(x+offset+P.imgCache[P.curPageIdx].width*s,y,P.PAGE_GAP,canv.height);
            cont.drawImage(P.imgCache[P.curPageIdx+1],
                0, 0,
                P.imgCache[P.curPageIdx+1].width, P.imgCache[P.curPageIdx+1].height,
                x+offset+P.PAGE_GAP+P.imgCache[P.curPageIdx].width*s, y,
                P.imgCache[P.curPageIdx+1].width*s, P.imgCache[P.curPageIdx+1].height*s);
            // 描边
            //cont.strokeRect(x+offset+P.PAGE_GAP+P.imgCache[P.curPageIdx].width*s+cont.lineWidth/2,
            //    y+cont.lineWidth/2,
            //    P.imgCache[P.curPageIdx+1].width*s-cont.lineWidth,
            //    P.imgCache[P.curPageIdx+1].height*s-cont.lineWidth);
        }
        // 上一页
        else if (offset>0 && P.imgCache[P.curPageIdx-1]){
            // 间隔
            cont.clearRect(x+offset-P.PAGE_GAP,y,P.PAGE_GAP,canv.height);
            cont.drawImage(P.imgCache[P.curPageIdx-1],
                0, 0,
                P.imgCache[P.curPageIdx-1].width, P.imgCache[P.curPageIdx-1].height,
                x+offset-P.PAGE_GAP-P.imgCache[P.curPageIdx-1].width*s, y,
                P.imgCache[P.curPageIdx-1].width*s,P.imgCache[P.curPageIdx-1].height*s);
            // 描边
            //cont.strokeRect(x+offset-P.PAGE_GAP-P.imgCache[P.curPageIdx-1].width*s+cont.lineWidth/2,
            //    y+cont.lineWidth/2,
            //    P.imgCache[P.curPageIdx-1].width*s-cont.lineWidth,
            //    P.imgCache[P.curPageIdx-1].height*s-cont.lineWidth);
        }
    }
}

// 自动滑动
// flg: TO_NEXT,TO_CURR,TO_PRE
function autoSlide(flg, endCallback){
    if (P_TOUCH.autoSlider){
        clearInterval(P_TOUCH.autoSlider);
        P_TOUCH.autoSlider = null;
    }

    // 控制滑动速度
    var WPF = (flg == P_TOUCH.TO_CURR)? P.PAGE_W/10:P.PAGE_W/10;

    if ( ((P.curPageIdx == (P.imgCache.length-1)) && (flg == P_TOUCH.TO_NEXT))
        || (P.curPageIdx == 0 && (flg == P_TOUCH.TO_PRE)) ) {
        flg = P_TOUCH.TO_CURR;
    }

    P_TOUCH.autoSlider = setInterval(function(){
        // 滑动
        var bEnd = false;
        switch (flg) {
            case P_TOUCH.TO_CURR:
                if (P_TOUCH.offset <= 0){
                    P_TOUCH.offset = P_TOUCH.offset + WPF;
                } else {
                    P_TOUCH.offset = P_TOUCH.offset - WPF;
                }

                if (Math.abs(P_TOUCH.offset) <= WPF) {
                    P_TOUCH.offset = 0;
                    bEnd = true;
                }
                break;

            case P_TOUCH.TO_NEXT:
                P_TOUCH.offset = P_TOUCH.offset - WPF;

                if (Math.abs(Math.abs(P_TOUCH.offset) - P.PAGE_W - P.PAGE_GAP) <= WPF) {
                    P_TOUCH.offset = -(P.PAGE_W+P.PAGE_GAP);
                    bEnd = true;
                }
                break;

            case P_TOUCH.TO_PRE:
                P_TOUCH.offset = P_TOUCH.offset + WPF;
                if (Math.abs(Math.abs(P_TOUCH.offset) - P.PAGE_W - P.PAGE_GAP) <= WPF) {
                    P_TOUCH.offset = P.PAGE_W+P.PAGE_GAP;
                    bEnd = true;
                }
                break;
        }
        updCanv(P_TOUCH.offset);

        // 滑动完成
        if (bEnd){
            clearInterval(P_TOUCH.autoSlider);
            P_TOUCH.autoSlider = null;

            switch (flg) {
                case P_TOUCH.TO_CURR:
                    break;
                case P_TOUCH.TO_NEXT:
                    P.curPageIdx++;
                    break;
                case P_TOUCH.TO_PRE:
                    P.curPageIdx--;
                    break;
            }
            P_TOUCH.offset = 0;
            if (endCallback && typeof(endCallback) === "function") {
                endCallback();
            }
            return;
        }

    }, 1000/P.FPS);
}


function onClickMake(){
    window.location.href = gAPIHOST+'/preview/makeCard';
}
function onClickDisplay(){
    if (P.dispTimer) {
        clearTimeout(P.dispTimer);
        P.dispTimer = null;
    }

    P.dispStatus = 1;// play
    $(".icon-music").removeClass("music_play");
    $(".icon-music").addClass("music_pause");
    $(".mask").addClass("display_hide");

    // 返回第一页
    if (P.curPageIdx > 0) {
        P.curPageIdx = 0;
        autoSlide(P_TOUCH.TO_CURR,onSlideChangeEnd);
        return;
    }

    // 播放音视频
    if (gMediaFlg == 1) {
        // 如果未在 onImagesReady 初始化 SJGMediaPlayer
        // 则按照窗口尺寸设置canvas位置
        if (gPlayer == null) {
            initMedia();
        }

        // loading遮罩层
        var playFlg = false;
        $(".mask2").removeClass("display_hide");
        var t = gPlayer.cache(0,function(ret){
            if (!playFlg) {
                playFlg = true;
                $(".mask2").addClass("display_hide");
                if (t == 0 || ret == 1){
                    dispAndSlide(P.DISP_DURATION);
                } else {
                    musicON(P.DELAY_1, 0);
                }
            }
        });

        // 等待30秒，若loading未成功则改为仅播放普通文档模式
        var wait = setTimeout(function(){
            //$(".mask2").addClass("display_hide");
            //if (!playFlg) {
            //    playFlg = true;
            //    gMediaFlg = 0;// 不播放音视频
            //    dispAndSlide(P.DISP_DURATION);
            //}
            if (!playFlg) {
                playFlg = true;
                $("#load_txt").addClass("display_hide");
                $("#timeout_div").removeClass("display_hide");
            }
        },30000);

    } else {
        dispAndSlide(P.DISP_DURATION);
    }
}

function onClickTimeout(){
    $(".mask2").addClass("display_hide");
    gMediaFlg = 0;// 不播放音视频
    dispAndSlide(P.DISP_DURATION);
}

function dispAndSlide(delay){
    if (P.dispTimer) {
        clearTimeout(P.dispTimer);
        P.dispTimer = null;
    }

    if (P.dispStatus == 1) {// play
        P.dispTimer = setTimeout(function(){
            if (P.curPageIdx == (P.imgCache.length-1)) {
                // 最后一页
                setReplayMask();
            } else {
                autoSlide(P_TOUCH.TO_NEXT, onSlideChangeEnd);
            }
        },delay);
    }
}

function clearMediaCanv() {
    var canv=document.getElementById('vid_canv');
    var cont=canv.getContext('2d');
    var maxH = document.body.clientHeight/2;
    cont.clearRect(-1,-1,canv.width+1,canv.height+1);
}

/**
 * play/pause 控制按钮
 */
function onMusicClick(){
    if(P.dispStatus == 1){// 点击前为play
        if (P.dispTimer) {
            clearTimeout(P.dispTimer);
            P.dispTimer = null;
        }

        P.dispStatus = 0;
        $(".icon-music").removeClass("music_pause");
        $(".icon-music").addClass("music_play");
        musicOFF();

    }else{// 点击前为pause
        P.dispStatus = 1;
        $(".icon-music").removeClass("music_play");
        $(".icon-music").addClass("music_pause");

        if (gMediaFlg == 1) {
            var t = gPlayer.cache(P.curPageIdx,function(ret){
                if (ret == 1) {// RET_ERROR
                    // TODO
                }
            });
            if (t == 1){
                musicON(P.DELAY_1, 1);// 恢复play
            } else {
                dispAndSlide(P.DISP_DURATION);
            }

        } else {
            dispAndSlide(P.DISP_DURATION);
        }
    }
}

/**
 * @param delayTime
 * @param flg: 0重新播放, 1继续播放
 */
function musicON(delayTime,flg) {
    if (gMediaFlg == 1) {
        setTimeout(function(){

            //if (gPagesJson && gPagesJson.length>0
            //    && gPagesJson[galleryTop.activeIndex].video
            //    && gPagesJson[galleryTop.activeIndex].video.length>0) {
            //    // 有视频时显示canvas
            //    $(".video").removeClass("display_hide");
            //}

            gPlayer.play(P.curPageIdx,flg,
                function(ret){
                    $(".mask2").addClass("display_hide");
                    if (ret == 1){// RET_ERROR
                        $(".video").addClass("display_hide");
                        clearMediaCanv();
                        dispAndSlide(P.DISP_DURATION);
                    } else {
                        $(".video").addClass("display_hide");
                        clearMediaCanv();
                        dispAndSlide(P.DELAY_1);
                    }
                });
        },delayTime);
    }
}
function musicOFF() {
    if (gMediaFlg == 1) {
        gPlayer.pause();
    }
}

function onSlideChangeEnd(){
    if (P.dispTimer) {
        clearTimeout(P.dispTimer);
        P.dispTimer = null;
    }

    // 清除canvas
    $(".video").addClass("display_hide");
    clearMediaCanv();

    if (P.dispStatus == 1) {// play
        if (gMediaFlg == 1) {
            musicOFF();
            var t = gPlayer.cache(P.curPageIdx,function(ret){
                if (ret == 1) {// RET_ERROR
                    // TODO
                }
            });
            if (t == 1){
                musicON(P.DELAY_1, 0);
            } else {
                dispAndSlide(P.DISP_DURATION);
            }

        } else {
            dispAndSlide(P.DISP_DURATION);
        }
    } else {// pause
        musicOFF();
    }
}

function initMedia(){
    if (gPlayer) {
        return;
    }

    gPlayer = new SJGMediaPlayer("vid_canv");
    var wh = gPlayer.init(gPagesJson);
    if (wh) {
        gMediaWhRatio = wh.w / wh.h;
        gMediaH = wh.h;

        // 设定视频canvas
        setMediaPos();
    }
}

function setMediaPos(){
    // 页面canvas
    var canv = document.getElementById('canv');

    // 设定视频canvas尺寸
    var vidCanv = document.getElementById('vid_canv');
    var vidCont = vidCanv.getContext('2d');

    // 缩放视频到指定大小
    if (gIsPortrait) {
        vidCanv.width = canv.width*0.4 || document.body.clientWidth*0.4;
        vidCanv.height = vidCanv.width * gMediaWhRatio;
    } else {
        vidCanv.height = canv.height*0.4 || document.body.clientHeight*0.4;
        vidCanv.width = vidCanv.height * gMediaWhRatio;
    }

    // 设定视频位置
    var vidDiv = document.getElementById('vid_canv_div');
    vidDiv.style.width = vidCanv.width + "px";
    vidDiv.style.height = vidCanv.height + "px";

    var top = 0;
    var left = 0;
    vidDiv.style.top = vidDiv.style.left = vidDiv.style.right = vidDiv.style.bottom = null;
    if (MediaPos == 5) {
        // 位移到图像中心
        top = (document.body.clientHeight - vidCanv.height)/2;
        left = (document.body.clientWidth - vidCanv.width)/2;

    } else {
        // 位移到图像右下角(边距10px)
        if (gIsPortrait) {
            top = (document.body.clientHeight + canv.height)/2 - vidCanv.height - 10;
            left = (document.body.clientWidth - canv.width)/2 + 10;
        } else {
            top = (document.body.clientHeight + canv.height)/2 - vidCanv.height - 10;
            left = (document.body.clientWidth + canv.width)/2 - vidCanv.width - 10;
        }
    }

    vidDiv.style.top = top + "px";
    vidDiv.style.left = left + "px";
}

function setReplayMask(){
    if (P.dispTimer) {
        clearTimeout(P.dispTimer);
        P.dispTimer = null;
    }

    P.dispStatus = 0;// pause
    $(".icon-music").removeClass("music_pause");
    $(".icon-music").addClass("music_play");

    $(".disp_play").addClass("display_hide");
    $(".disp_replay").removeClass("display_hide");

    $(".mask").removeClass("display_hide");
}


wx.ready(function () {
    // 自定义分享内容及分享结果接口
    var vm = (gMediaFlg == 1)?3:2;

    var myAppMsg = {
        title: gWechat.title,
        desc: gWechat.desc,
        link: gURL,
        imgUrl: gShareIcon,
        trigger: function (res) {
            //alert('用户点击发送给朋友');
        },
        success: function (res) {
            if (gMyParam && gMyParam.di && gMyParam.ui) {
                var postUrl = gAPIHOST+'/preview/usi';
                var sendData = {
                    di:gMyParam.di,
                    ui:gMyParam.ui,
                    rm:1,
                    vm:vm,
                };
                $.post(postUrl, sendData, function(ret){
                    //
                });
            }
            alert('已分享');
        },
        cancel: function (res) {
            alert('已取消');
        },
        fail: function (res) {
            alert(JSON.stringify(res));
        }
    };

    var myTimeline = {
        title: gWechat.title,
        desc: gWechat.desc,
        link: gURL,
        imgUrl: gShareIcon,
        trigger: function (res) {
            //alert('用户点击分享朋友圈');
        },
        success: function (res) {
            if (gMyParam && gMyParam.di && gMyParam.ui) {
                var postUrl = gAPIHOST+'/preview/usi';
                var sendData = {
                    di:gMyParam.di,
                    ui:gMyParam.ui,
                    rm:2,
                    vm:vm,
                };
                $.post(postUrl, sendData, function(ret){
                    //
                });
            }
            alert('已分享');
        },
        cancel: function (res) {
            alert('已取消');
        },
        fail: function (res) {
            alert(JSON.stringify(res));
        }
    };

    // 监听“分享给朋友”
    wx.onMenuShareAppMessage(myAppMsg);
    // 监听“分享到朋友圈”
    wx.onMenuShareTimeline(myTimeline);

});

wx.error(function (res) {
    //alert(res.errMsg);
});